{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import pickle\n",
    "from sklearn.preprocessing import minmax_scale\n",
    "from keras.models import load_model\n",
    "\n",
    "# parameters\n",
    "max_features = 1024\n",
    "batch_size = 64\n",
    "\n",
    "# load models\n",
    "cnn_model = load_model('CNN_model.h5')\n",
    "xgb_model = pickle.load(open(\"xgb.model.dat\", \"rb\"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# fit cnn model\n",
    "test = pd.read_csv('data/test.csv', header=None, names = list(range(0,max_features)))\n",
    "\n",
    "# cnn_test = test.fillna(0)\n",
    "# cnn_test = cnn_test.iloc[:, :]\n",
    "\n",
    "# cnn_pred = cnn_model.predict(cnn_test, batch_size=batch_size)\n",
    "# cnn_df = pd.DataFrame(cnn_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# fit xgboost model\n",
    "test_copy = test\n",
    "number_of_nan = test_copy.isnull().sum(axis=1)\n",
    "test_copy = test_copy.fillna(0)\n",
    "test_copy = test_copy.astype(int)\n",
    "number_valid = max_features - number_of_nan\n",
    "test_describe = test_copy.apply(pd.DataFrame.describe, axis=1)\n",
    "test_copy = pd.concat([test_copy, number_of_nan, number_valid, test_describe], axis=1)\n",
    "test_copy.columns.values[1024] = \"number_of_nan\"\n",
    "test_copy.columns.values[1025] = \"number_valid\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "submission_df = pd.read_csv('data/sample_submission.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "cnn_df = pd.read_csv('data/sample_submission_cnn.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "xgb_pred = xgb_model.predict_proba(test_copy,)[:, 1]\n",
    "xgb_df = pd.DataFrame(xgb_pred)\n",
    "\n",
    "# Ensemble\n",
    "probs = 0.5*cnn_df.iloc[:,1] + 0.5*xgb_df.iloc[:,0]\n",
    "\n",
    "ensemble = pd.DataFrame({\n",
    "    'sample_id': submission_df.iloc[:,0],\n",
    "    'malware': probs\n",
    "}, columns=['sample_id', 'malware'])\n",
    "\n",
    "ensemble.to_csv(\"data/sample_submission_ensemble.csv\",  index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
